package com.atguigu.yygh.user.controller.user;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author meihua
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/user/userinfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    /**
     * @description:用户端登录操作----------------------------5004
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/18
     * @param  :1.8
     */


    /**
     * @description:day14天，实名认证
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/19
     * @param  :1.8  /添加||修改updateById这里要注意
     */
    @PutMapping("/update")
    public R update(@RequestHeader String token, UserAuthVo userAuthVo){
        Long userId = JwtHelper.getUserId(token);
        UserInfo userInfo=new UserInfo();
        userInfo.setId(userId);
        userInfo.setName(userAuthVo.getName());
        userInfo.setCertificatesType(userAuthVo.getCertificatesType());
        userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
        userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());
        userInfoService.updateById(userInfo);

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        Map<String,Object> map = userInfoService.login(loginVo);
        return R.ok().data(map);
    }

    //获取用户信息  请求头的信息token //做的是用户认证
    @GetMapping("/info")
    public R getUserInfo(@RequestHeader String token ){
        Long userId = JwtHelper.getUserId(token);
        UserInfo byId = userInfoService.getUserInfo(userId);
        return R.ok().data("user",byId);
    }
}

